//
//  HomePageViewController.m
//  大轮胎
//
//  Created by 朱永超 on 2017/3/16.
//  Copyright © 2017年 zyc. All rights reserved.
//

#import "HomePageViewController.h"
//引入头文件
#import <SDCycleScrollView.h>
#import "YCNetWork.h"
#import "HomeModel.h"
#import "HomeBtnView.h"
#import <Masonry.h>
#import "HomeCollectionViewCell.h"
#import "HomeBtnView.h"
#import "HomeCollectionView.h"
#import "HomeTableViewCell.h"
#import <UITableView+FDTemplateLayoutCell.h>
//宏
#define SCROLL_IMGURL @"http://lscy4.caeac.com.cn/api/home_update.php?appkey=1138&time=1433337484"  //首页
#define SHOP @"http://lscy4.caeac.com.cn/api/partner.php?appkey=1138" //商铺
@interface HomePageViewController ()
{
    NSMutableArray *scrollImageURLGroup;  //滚动视图内容标题数组
    NSMutableArray *collectionArr;        //网格视图内容数组
    HomeBtnView *homeBtn;                 //4个按钮
    HomeCollectionView *collectionV;      //网格视图
    NSMutableArray *shopArr;              //商铺内容数组
    UIView *headView;                     //头视图View
}
@end

@implementation HomePageViewController
- (void)viewDidLoad {
    [super viewDidLoad];
    //调用设置导航栏
    [self navigation];
    //获取网络数据
    [self networking];
    
    [self.tableView registerClass:[HomeTableViewCell class] forCellReuseIdentifier:@"cell"];
    self.tableView.tableFooterView = [[UIView alloc] init];
    //表格头视图
    [self tabHeaderView];
    //[self net];
}
//设置导航栏
-(void)navigation
{
    //设置标题
    self.navigationItem.title = @"轮胎商城";
    //设置导航标题字体大小和颜色
    [self.navigationController.navigationBar setTitleTextAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:17],
        NSForegroundColorAttributeName:[UIColor whiteColor]}];
    
    
    
    
    //设置背景颜色
    self.view.backgroundColor = [UIColor whiteColor];
}
-(void)net
{
    NSString *url = @"http://192.168.1.105:8888/returnData.php";
    NSDictionary *parameters = @{@"type" : @(2)};
    [YCNetWork postWithURLString:url parameters:parameters success:^(id data) {
        NSLog(@"%@",data);
        
    } failure:^(NSError *error) {
        
        
    }];
    NSString *url1 = @"http://192.168.1.105:8888/GetRequest.php";
    [YCNetWork getWithURLString:url1 parameters:nil success:^(id data) {
        
        NSLog(@"--------------%@",data);
    } failure:^(NSError *error) {
        
        
    }];
}
//获取网络数据
-(void)networking
{
    //首页
    [YCNetWork getWithURLString:SCROLL_IMGURL parameters:nil success:^(id data) {
        NSArray *json = [data objectForKey:@"json"];
        //滚动视图内容数组
        scrollImageURLGroup = [NSMutableArray array];
        //网歌视图内容数组
        collectionArr = [NSMutableArray array];
        for (NSDictionary *dic in json)
        {
            /**
                存储首页类型的model
             */
            HomeModel *homeM = [[HomeModel alloc] init];
            [homeM setValuesForKeysWithDictionary:dic];
            /**
             存储滚动视图内容的model
             */
            if ([homeM.type isEqualToString:@"scroll_ad"])
            {
                for (NSDictionary *dic in homeM.subClass)
                {
                    ScrollModel *scrollM = [[ScrollModel alloc] init];
                    [scrollM setValuesForKeysWithDictionary:dic];
                    [scrollImageURLGroup addObject:scrollM.img];
                    
                }
            }
            /**
             存储网格视图内容的model
             */
            if ([homeM.type isEqualToString:@"product"] || [homeM.type isEqualToString:@"product2"])
            {
                for (NSDictionary *dic in homeM.subClass) {
                    CollectionModel *collectionM = [[CollectionModel alloc] init];
                    [collectionM setValuesForKeysWithDictionary:dic];
                    [collectionArr addObject:collectionM];
                }
            }
        }
        // 网络加载图片的轮播器
        SDCycleScrollView *cycleScrollView = [SDCycleScrollView cycleScrollViewWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 200) imageURLStringsGroup:scrollImageURLGroup];
        [headView addSubview:cycleScrollView];
        [headView layoutIfNeeded];
        [collectionV.homeCollectV reloadData];
    } failure:^(NSError *error) {
        
    }];
    
    //商铺
    [YCNetWork getWithURLString:SHOP parameters:nil success:^(id data) {
        NSArray *partner = [data objectForKey:@"partner"];
        shopArr = [NSMutableArray array];
        for (NSDictionary *dic in partner) {
            ShopModel *shopM = [[ShopModel alloc] init];
            [shopM setValuesForKeysWithDictionary:dic];
            [shopArr addObject:shopM];
        }
        [self.tableView reloadData];
    } failure:^(NSError *error) {
        
        
    }];
    
}

//表格头视图
-(void)tabHeaderView
{
    headView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 620)];
    
    //四个按钮
    homeBtn = [[HomeBtnView alloc] initWithFrame:CGRectMake(0, 200, [UIScreen mainScreen].bounds.size.width, 90)];
    [headView addSubview:homeBtn];
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 290, [UIScreen mainScreen].bounds.size.width, 10)];
    label.backgroundColor = [UIColor colorWithRed:248/255.0 green:248/255.0 blue:248/255.0 alpha:1];
    [headView addSubview:label];
    //网格视图
    collectionV = [[HomeCollectionView alloc] initWithFrame:CGRectMake(0, 300, [UIScreen mainScreen].bounds.size.width, 320)];
    collectionV.homeCollectV.delegate = self;
    collectionV.homeCollectV.dataSource = self;
    [collectionV.homeCollectV registerClass:[HomeCollectionViewCell class] forCellWithReuseIdentifier:@"collection"];
    [headView addSubview:collectionV];
    
    self.tableView.tableHeaderView = headView;
}
#define mark - UITableViewDataSource
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return shopArr.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    HomeTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    ShopModel *shopM = shopArr[indexPath.row];
    cell.shopModel = shopM;
    
    return cell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    //自适应高度
    return [tableView fd_heightForCellWithIdentifier:@"cell" configuration:^(HomeTableViewCell *cell) {
        ShopModel *shopM = shopArr[indexPath.row];
        cell.shopModel = shopM;
    }];
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [self.tableView deselectRowAtIndexPath:indexPath animated:NO];
}
#define UICollectionViewDataSource
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return collectionArr.count;
}
- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    HomeCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"collection" forIndexPath:indexPath];
    
    CollectionModel *collectionM = collectionArr[indexPath.item];
    cell.collectionM = collectionM;
    
    return cell;
}

@end
